package com.atguigu.tingshu.user.strategy.impl;

import com.atguigu.tingshu.album.client.TrackInfoFeignClient;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.model.user.UserPaidTrack;
import com.atguigu.tingshu.user.mapper.UserPaidTrackMapper;
import com.atguigu.tingshu.user.strategy.ItemTypeStrategy;
import com.atguigu.tingshu.vo.user.UserPaidRecordVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author atguigu-mqx
 * @ClassName VipStrategy
 * @description: TODO
 * @date 2025年03月10日
 * @version: 1.0
 */
@Service("1002")
@Slf4j
public class TrackStrategy implements ItemTypeStrategy {

    @Autowired
    private TrackInfoFeignClient trackInfoFeignClient;

    @Autowired
    private UserPaidTrackMapper userPaidTrackMapper;

    @Override
    public int savePaidRecord(UserPaidRecordVo userPaidRecordVo) {
        //  声明一个变量;
        int result = 0;
        if (SystemConstant.ORDER_ITEM_TYPE_TRACK.equals(userPaidRecordVo.getItemType())) {
            //  购买的声音Id;
            List<Long> itemIdList = userPaidRecordVo.getItemIdList();
            //  根据声音Id获取声音对象;
            Result<TrackInfo> trackInfoResult = trackInfoFeignClient.getTrackInfo(itemIdList.get(0));
            Assert.notNull(trackInfoResult, "查询声音失败！");
            TrackInfo trackInfo = trackInfoResult.getData();
            Assert.notNull(trackInfo, "查询声音失败！");
            //  查询当前用户是否购买过声音; select * from user_paid_track where user_id = 30 and track_id in (1,2,3);
            //  一样的就不需要插入数据; 可能涉及到退款；否则就插入数据！
            List<Long> userPaidTrackList = userPaidTrackMapper.selectList(new LambdaQueryWrapper<UserPaidTrack>().eq(UserPaidTrack::getUserId, userPaidRecordVo.getUserId()).in(UserPaidTrack::getTrackId, itemIdList)).stream().map(UserPaidTrack::getTrackId).collect(Collectors.toList());
            //  循环遍历;
            for (Long trackId : itemIdList) {
                if (userPaidTrackList.contains(trackId)) {
                    //  退款; sendMsg();
                    log.warn("退款:" + trackId);
                } else {
                    //  插入数据；
                    UserPaidTrack userPaidTrack = new UserPaidTrack();
                    userPaidTrack.setUserId(userPaidRecordVo.getUserId());
                    userPaidTrack.setTrackId(trackId);
                    userPaidTrack.setOrderNo(userPaidRecordVo.getOrderNo());
                    //  获取专辑Id;
                    userPaidTrack.setAlbumId(trackInfo.getAlbumId());
                    //  保存数据;
                    result += userPaidTrackMapper.insert(userPaidTrack);
                }
            }
        }
        return result;
    }
}
